package com.yoke.controller.tongji;

import com.alibaba.fastjson.JSONObject;
import com.yoke.common.JsonMessage;
import com.yoke.common.base.NginxBaseControl;
import com.yoke.database.page.Page;
import com.yoke.database.page.SqlUtil;
import com.yoke.system.log.SystemLog;
import com.yoke.util.DateUtil;
import com.yoke.util.StringUtil;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.sql.SQLException;

/**
 * Created by jiangzeyin on 2017/8/11.
 */
@Controller
@RequestMapping("tongji")
public class TableControl extends NginxBaseControl {

    @RequestMapping(value = "table.html", produces = MediaType.TEXT_HTML_VALUE)
    public String details(String url) throws IOException {
        return "tongji/table";
    }

    @RequestMapping(value = "table_data.json", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String table_data(String url, String startTime, String endTime, String pageNo) throws IOException {
        if (StringUtil.IsEmpty(url))
            return JsonMessage.getString(100, "链接不正确");
        if (!url.startsWith("http://"))
            return JsonMessage.getString(100, "链接不正确,不是有效的http");
        String tempUrl = url.substring(url.indexOf("http://") + 7);
        int s = tempUrl.indexOf("/");
        if (s <= -1)
            return JsonMessage.getString(100, "链接不正确,-1");
        int e = tempUrl.lastIndexOf("/");
        if (s < e) {
            url = url.substring(0, e + 1 + 7);
        }
        int startTime_int = 0;
        if (!StringUtil.IsEmpty(startTime)) {
            startTime = startTime + " 00:00:00";
            startTime_int = DateUtil.formatTime(null, startTime);
            if (startTime_int <= 0)
                return JsonMessage.getString(140, "查询的开始时间不正确:" + startTime);
        }
        int endTime_int = 0;
        if (!StringUtil.IsEmpty(endTime)) {
            endTime += " 23:59:59";
            endTime_int = DateUtil.formatTime(null, endTime);
            if (endTime_int <= 0)
                return JsonMessage.getString(140, "查询的结束时间不正确：" + endTime);
        }
        return doSelect(url, pageNo, startTime_int, endTime_int);
    }

    private String doSelect(String url, String pageNo, int startTime, int endTIme) {
        int pageNo_int = StringUtil.parseInt(pageNo, 1);
        int pageSize_int = 10;
        Page page = new Page("select * from nginxAdLog");
        page.setPageNoAndSize(pageNo_int, pageSize_int);
        page.setWhereWord("url LIKE '" + url + "%'");
        if (startTime > 0) {
            page.setWhereWord("time>=" + startTime);
        }
        if (endTIme > 0) {
            page.setWhereWord("time<=" + endTIme);
        }
        page.setOrderBy("time desc");
        try {
            SqlUtil.doPage(page);
        } catch (SQLException e) {
            SystemLog.ERROR().error("分页错误", e);
            return JsonMessage.getString(500, "系统异常");
        }
        JSONObject result = new JSONObject();
        result.put("pageNo", page.getPageNo());
        result.put("pageSize", page.getPageSize());
        result.put("totalPage", page.getTotalPage());
        result.put("totalRecord", page.getTotalRecord());
        result.put("results", page.getMapList());
        return JsonMessage.getString(200, "获取成功", result);
    }
}
